package com.netease.financial.common.util;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * Created by wyf on 4/5/16.
 */
public class URIUtils {

    public static URI appendUri(String uri, String appendQuery) throws URISyntaxException {
        URI oldUri = new URI(uri);

        String newQuery = oldUri.getQuery();
        if (newQuery == null) {
            newQuery = appendQuery;
        } else {
            newQuery += "&" + appendQuery;
        }

        URI newUri = new URI(oldUri.getScheme(), oldUri.getAuthority(), oldUri.getPath(), newQuery, oldUri.getFragment());
        return newUri;
    }

    public static String appendUriQuietly(String uri, String appendQuery) {
        String newUriString;
        try {
            newUriString = appendUri(uri, appendQuery).toString();
        } catch (URISyntaxException e) {
            e.printStackTrace();
            newUriString = uri;
        }
        return newUriString;
    }
}
